package com.lancoo.skegn;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.tt.CoreType;
import com.tt.QType;
import com.tt.SkEgnManager;
import com.tt.listener.OnRecordListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.UUID;

/**
 * Created by pengmi on 2018/11/21.
 * Description:语音识别
 */

public class LancooSpeechRecognitionManager {

    public static LancooSpeechRecognitionManager lancooSpeechRecognitionManager;
    private Context mContext;
    private OnLancooSpeechRecognitionListener onLancooSpeechRecognitionListener;
    private boolean isShowSpeechResult,isEnableRecogin;

    private LancooSpeechRecognitionManager(){

    }

    public static LancooSpeechRecognitionManager getInstance(){
        if(lancooSpeechRecognitionManager==null){
            lancooSpeechRecognitionManager=new LancooSpeechRecognitionManager();
        }
        return lancooSpeechRecognitionManager;
    }


    public void startSpeechRecognition(Context mContext,OnLancooSpeechRecognitionListener onLancooSpeechRecognitionListener){
        this.mContext=mContext;
        this.onLancooSpeechRecognitionListener=onLancooSpeechRecognitionListener;
        isShowSpeechResult=true;
        isEnableRecogin=true;
        SkEgnManager.getInstance(mContext).startRecord( CoreType.EN_ASR_REC, "111", QType.QTYPE_EMPTY, getTempRecordFilePath(UUID.randomUUID()+""),mOnRecordListener);
    }


    public void stopSpeechRecognition(boolean isShowSpeechResult){
        this.isShowSpeechResult=isShowSpeechResult;
        SkEgnManager.getInstance(mContext).stopRecord();

    }

    OnRecordListener mOnRecordListener = new OnRecordListener() {
        @Override
        public void onRecordStart() {
            //开始录制
            if(onLancooSpeechRecognitionListener!=null){
                onLancooSpeechRecognitionListener.onSpeechRecognitionStart();
            }
        }

        @Override
        public void onRecording(int vad_status, int sound_intensity) {
            //录制中
            if(onLancooSpeechRecognitionListener!=null){
                onLancooSpeechRecognitionListener.onSpeechRecogniting(vad_status,sound_intensity);
            }
        }
        @Override
        public void onRecordEnd(String result) {
            if(isEnableRecogin){
                isEnableRecogin=false;
                try{
                    if(isShowSpeechResult){
                        final JSONObject json = new JSONObject(result);
                        String recognition = json.getJSONObject("result").getString("recognition");
                        String path = SkEgnManager.getInstance(mContext).getPath();
                        if(onLancooSpeechRecognitionListener!=null&&!TextUtils.isEmpty(path)){
                            onLancooSpeechRecognitionListener.onSpeechRecognitionEnd(recognition,path);
                        }else{
                            onLancooSpeechRecognitionListener.onSpeechRecognitionError("path is empty!");
                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    if(onLancooSpeechRecognitionListener!=null&&isShowSpeechResult){
                        onLancooSpeechRecognitionListener.onSpeechRecognitionError(e.toString());
                    }
                }
            }
        }
    };

    public String getTempRecordFilePath(String recorderFileName){
        File targetDir = new File(Environment.getExternalStorageDirectory(),
                "Android/data/" + mContext.getApplicationInfo().packageName + "/files/record/klg");
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
//        File mRecordTempDir=new File(targetDir.getAbsolutePath(),"klg");
//        if(!mRecordTempDir.exists()){
//            mRecordTempDir.mkdir();
//        }
        Log.e("aaa","path:"+targetDir.getPath());
        return targetDir.getPath()+"/"+recorderFileName+".wav";
    }

    public interface OnLancooSpeechRecognitionListener{
        void onSpeechRecognitionStart();
        void onSpeechRecogniting(int vad_status, int sound_intensity);
        void onSpeechRecognitionEnd(String recognitionSentence, String url);
        void onSpeechRecognitionError(String error);
    }


}
